#ifndef __T90type__
#define __T90type__

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "datetime.h"
#include "board_cfg.h"
#include "view.h"
#include "bit_func.h"
#define ANCS_SWITCH_BITS 32
#define SPORT 0x00
#define SLEEP 0x01
#define STOP 0x02

// #define POWEROFFING 0
#define POWEROFFOK 1

// #define POWERONING 2
#define POWERONOK 3

#define HeartRateSate 0
#define BloodpressureSate 1
#define BloodoxygenSate 2
#define TiredpressureSate 3

#define UI_SHOW_AUTO_OFF_TIME_MS (5000)
#define UI_SHOW_AUTO_OFF_TIME_MS_MIN (1000)
#define UI_SHOW_AUTO_OFF_TIME_MS_MAX (20000)

//#define  UISateDefult         -1
typedef enum
{
    HR_STATE_STANDBY,  //空闲中
    HR_STATE_PRE_TEST, //预测阶段
    HR_STATE_IN_TEST,  //测量中
    HR_STATE_NOT_WEAR, //未佩戴中
} hr_state_t;

enum
{
    USER_HOME_PAGE_IMG_TYPE_DEFUALT,
    USER_HOME_PAGE_IMG_TYPE_USER,
};
typedef struct
{
    int8_t PowerState; //当前电源的状态：  1：已关机  3：已开机
    hr_state_t hr_state;
    uint8_t time_sync;         //时间同步标记 0=未同步，1=已同步
    uint8_t hr_ble_tx_enable;  //开启蓝牙透传心率数据
    uint8_t hr_step_tx_enable; //开启心率和步频发送
    uint8_t time_sec_add_step;
    // uint8_t user_home_page_img_type; //用户首页的图片配置
    bool wait_for_reset;
} system_state_t;
extern system_state_t DeviceState; //结构体变量在main.c内定义

#define Lefthand 0x01
#define Righand 0x02
#define TurnWrist 0x04
#define None 0x00

typedef struct
{
    uint8_t enable; // 翻腕开关，1=开启，其他（0/2）都是关闭
    uint8_t Hand;
} MOVEHAND;
extern MOVEHAND MoveHand;
// 打印免打扰区间信息
typedef enum
{
    DONT_DISTURB_REFRESH,
    DONT_DISTURB_CHECK,
    DONT_DISTURB_APP_SET
} dont_disturb_info_type_e;

typedef enum
{
    CAMERA_CMD_SLAVE_INTOCAMERA = 1,
    CAMERA_CMD_SLAVE_STARTPHOTO,
    CAMERA_CMD_SLAVE_LEVECAMERA,
    CAMERA_CMD_MASTER_INTOCAMERA,
    CAMERA_CMD_MASTER_STAYCAMERA,
    CAMERA_CMD_MASTER_LEAVECAMERA,
} camera_cmd_t;

//音乐控制子界面状态

enum
{
    MUSIC_SUB_STATE_STANDBY,
    MUSIC_SUB_STATE_VOL_DEC,
    MUSIC_SUB_STATE_LAST,
    MUSIC_SUB_STATE_PLAY,
    MUSIC_SUB_STATE_NEXT,
    MUSIC_SUB_STATE_VOL_ADD,
    MUSIC_SUB_STATE_RETURN,
};

// HID 音乐控制指令
enum
{
    MEDIA_PLAYER_CTRL_PLAY_PAUSE = 1,   //播放暂停
    MEDIA_PLAYER_CTRL_START_APPL = 2,   //启动应用
    MEDIA_PLAYER_CTRL_NEXT = 4,         //下一首
    MEDIA_PLAYER_CTRL_LAST = 8,         //上一首
    MEDIA_PLAYER_CTRL_VOICE_DEC = 0x10, //音量减
    MEDIA_PLAYER_CTRL_VOICE_ADD = 0x20, //音量加
    MEDIA_PLAYER_CTRL_APPL_CTRL = 0x40, //应用控制
    MEDIA_PLAYER_CTRL_RETURN = 0x80,    //返回
};

// enum
// {
//     SPORT_SUB_STATE_LAST_RECORD,
//     SPORT_SUB_STATE_RUN,
//     SPORT_SUB_STATE_RIDING,
//     SPORT_SUB_STATE_WALK,
//     SPORT_SUB_STATE_EXERCISE,
//     SPORT_SUB_STATE_SWIM,
//     SPORT_SUB_STATE_BADMINTON,
//     SPORT_SUB_STATE_RETURN,
//     SPORT_SUB_STATE_END,

//     SPORT_SUB_STATE_TREADMILL, //未使用
// };

typedef enum
{
    APP_HEALTH_TEST_ONCE_HR = 1,    //单次心率测量
    APP_HEALTH_TEST_BLOOD_PRESSURE, //血压测量
    APP_HEALTH_TEST_OXIMETER,       //血氧测量
    APP_HEALTH_TEST_BLOOD_GLUCOSE,  //血糖
    APP_HEALTH_TEST_CHOLESTREROL,   //胆固醇

} app_health_test_type_t;

typedef enum
{
    APP_REALTIME_HR_TEST_STATE_OFF,      //关闭
    APP_REALTIME_HR_TEST_STATE_ON,       //开始
    APP_REALTIME_HR_TEST_STATE_PAUSE,    //暂停
    APP_REALTIME_HR_TEST_STATE_CONTINUE, //继续
    APP_REALTIME_HR_TEST_STATE_STOP,     //停止
} app_realtime_hr_test_state_t;

typedef enum
{
    PHONE_SYSTEM_TYPE_NONE,
    PHONE_SYSTEM_TYPE_IOS,     //手机IOS系统
    PHONE_SYSTEM_TYPE_ANDROID, //手机安卓系统
} phone_system_type_t;

typedef struct
{
    uint8_t code;
    uint8_t realtime_steps[3];         //
    uint8_t realtime_running_steps[3]; //
    uint8_t realtime_calorie[3];       //
    uint8_t realtime_distance[3];      //
    uint8_t realtime_sport_period[2];
    uint8_t crc;
} sport_info_now_packet_t; //

//修改前后填充字节，保持和cmd_packet一致
typedef __packed struct
{
    uint8_t reserve_byte;
    uint8_t num;
    uint8_t type;
    uint8_t hour;
    uint8_t minute;
    uint8_t enable_week_flag[7];
    uint8_t reserve[4];
} clock_packet_t;

//定时血压测量的参数结构体
typedef struct
{
    uint8_t rw_flag;
    uint8_t enable;
    uint8_t start_h;
    uint8_t start_m;
    uint8_t end_h;
    uint8_t end_m;
    uint8_t interval;
} bp_am_param_t;

typedef __packed struct
{
    uint8_t starthour;
    uint8_t startminute;
    uint8_t stophour;
    uint8_t stopminute;
    uint8_t enable_week_flag;
    uint8_t sporttime;
} longsit_packet_t;

//修改前后填充字节，保持和cmd_packet一致
typedef __packed struct
{
    uint8_t reserve_byte;
    uint8_t num;
    uint8_t type;
    uint8_t hour;
    uint8_t minute;
    uint8_t enable_week_flag[7];
    uint8_t reserve[4];
} drink_notify_packet_t;

typedef struct
{
    uint8_t code;
    uint8_t info_index;
    uint8_t date_index;
    date_t date;
    uint8_t steps[3];
    uint8_t running_steps[3];
    uint8_t calorie[3]; // ???
    uint8_t crc;
} sport_info_packet_t;

typedef struct
{
    uint8_t code;
    uint8_t info_index;
    uint8_t date_index;
    date_t date;
    uint8_t distance[3];
    uint8_t sport_period[2]; //
    uint8_t sleep_period[2]; //
    uint8_t reserve[2];
    uint8_t crc;
} sport_info_addition_packet_t;

typedef struct
{
    uint8_t code;
    uint8_t status[4];
    uint8_t reserve[10];
    uint8_t crc;
} sporthistory_status_packet_t;

typedef enum
{
    TIME_HOUR_SYSTEM_24,
    TIME_HOUR_SYSTEM_12,
} time_hour_system_e;

enum
{
    GENDER_TYPE_MAN,
    GSENDER_TYPE_WOMAN,
};

typedef enum __relax_option_time_t
{
    relax_option_time_2_min,
    relax_option_time_5_min,
} relax_option_time_t;

typedef enum __relax_option_shock_t
{
    relax_option_shock_false,
    relax_option_shock_true,
} relax_option_shock_t;

typedef enum __altitude_calibration_t
{
    altitude_calibration_auto,
    altitude_calibration_set,
} altitude_calibration_t;
typedef enum __language_t
{
    language_english,
    language_simplified_chinese,
    language_russian,
    language_german,
    language_japanese,
    language_french,
    language_portuguese,
    language_spanish,
    language_arabic,
    language_count,
} language_t;
typedef enum __Scan_mode
{
    Scan_mode_walnutin,
    Scan_mode_off,
    Scan_mode_any,
    Scan_mode_end,
} Scan_mode;
typedef struct
{
    uint8_t time_hour_system : 1; // 12(1)/24(0)小时制式
    uint8_t hr_auto_measure : 1;  //心率+
    uint8_t km_or_mi : 1;         //公英制，0=公制
    uint8_t ignore_infrared : 1;  //!< 屏蔽红外功能
    uint8_t reserved : 4;
    uint8_t home_page_type;         //首页样式
    uint16_t lcd_auto_off_delay_ms; //自动息屏时间

    uint32_t target_step;
    uint32_t target_calorie;
    uint32_t target_dist;

    uint8_t gender; //性别
    uint8_t age;
    uint8_t height;
    uint8_t weight;

    uint8_t relax_option_time;
    uint8_t relax_option_shock;
    uint16_t metronome_hz;
    float auto_sea_level_pressure;
    float manual_sea_level_pressure;
    float pressure_offset;
    altitude_calibration_t altitude_calibration_option;
    Scan_mode scan_mode;
    uint8_t warning_distance; //单位 *10 cm
    uint8_t reserved_1[1];
    uint32_t temperature_measure_interval; //秒 0表示未开启
    uint8_t ancs_switch[ARRAY_BIT_SIZE(ANCS_SWITCH_BITS)];
    uint8_t reserve[186 - sizeof(uint16_t) - 6 * sizeof(uint32_t) - 4 - 4 - 4 - 4 - 4 - 4];
    // uint8_t reserve[186 - sizeof(uint16_t) - 6 * sizeof(uint32_t) - 4 - 4 - 4];
    uint16_t crc;
} sys_cfg_data_t;

#define TARGET_STEP_MIN (100)
#define TARGET_STEP_DEFAULT (5000)
#define TARGET_CALORIE_DEFAULT (TARGET_STEP_DEFAULT * CONST_CALORIE * 1000 * 1.3)
#define TARGET_DIST_DEFAULT (TARGET_STEP_DEFAULT * CONST_DISTANCE * 0.8)
#define TARGET_SPORT_TIME_DEFAULT (2 * 60)
#define TARGET_SLEEP_TIME_DEFAULT (8 * 60)

#define USER_DEFAULT_HEIGHT (170)

#define SYS_CFG4_DEFAULT                                                                                                                                                                               \
    {                                                                                                                                                                                                  \
        .gender = GENDER_TYPE_MAN, .age = 30, .height = USER_DEFAULT_HEIGHT, .weight = 65, .sbp = 130, .dbp = 85, .hr_danger_line = 150,                                                               \
    }

#define CONST_CALORIE (0.05)                                    // 根据步数算卡路里的一个常量
#define CONST_DISTANCE (system_config_get_height() * 0.4 / 100) // 根据步数算里程的一个常量

//心率测量的模式
typedef enum
{
    HR_CTRL_TYPE_HR_TEST = 1,            //正常心率测量
    HR_CTRL_TYPE_WEAR_JUDGE = 2,         //佩戴检测
    HR_CTRL_TYPE_HEART_AUTO_MEASURE = 4, // 定时心率测量
    HR_CTRL_TYPE_SPORTS_MODE = 8,        // 定时心率测量
    HR_CTRL_TYPE_TEMPERATURE = 16,
    HR_CTRL_TYPE_TEMPERATURE_AUTO = 32,
    // HR_CTRL_TYPE_BP_AM      = 4,        //血压自动测量
    // HR_CTRL_TYPE_EXERCISE   = 8,            //血压自动测量
    // HR_CTRL_TYPE_HEART_AUTO_MEASURE = 0x10,   // 定时心率测量
    // HR_CTRL_TYPE_ECG_MEASURE = 0X20,
    HR_CTRL_TYPE_ALL = 0xff, //全部，关闭心率模块时会用到
} hr_test_type_t;

//闹钟提醒、喝水提醒的数量
#define CLOCK_ALERT_NUM 5
#define DRINK_ALERT_NUM 8

//新的FLASH分配
// BLOCK
#define FLASH_ALERT_BLOCK 0
//#define FLASH_SPORTSTATUS_BLOCK 0
//#define FLASH_SPORTHISTORY_BLOCK 2
#define FLASH_PRODUCT_TEST_BLOCK 0
#define FLASH_PRODUCT_TEST_FLAG_BLOCK 1
// OFFSET
// BLOCK0原有的
#define FLASH_CLCOK_START 0
#define FLASH_SITLONGTIME_START (16 * CLOCK_ALERT_NUM)                                      // 0x76000 + (dec:80 - hex:0x50)
#define FLASH_DRINKALERT_START (16 * CLOCK_ALERT_NUM + 16)                                  // 0x76000 + (96 - hex:0x60)
#define FLASH_MOVEHAND_START (16 * CLOCK_ALERT_NUM + 16 + 16 * DRINK_ALERT_NUM)             // 0x76000 + (224 - hex:0xE0)
#define FLASH_DISTRUB_START (16 * CLOCK_ALERT_NUM + 16 + 16 * DRINK_ALERT_NUM + 16)         // 0x76000 + (240 - hex:0xF0)
#define FLASH_ANCSSWITCH_START (16 * CLOCK_ALERT_NUM + 16 + 16 * DRINK_ALERT_NUM + 16 + 16) // 0x76000 + (256 - hex:0x100)
// BLOCK0新增加的

#define FLASH_PRODUCT_TEST_FLAG_START (FLASH_ANCSSWITCH_START + 16) // 0x76000 + (272 - hex:0x110)
#define FLASH_SN_START (FLASH_PRODUCT_TEST_FLAG_START + 4)          // 0x76000 + (276 - hex:0x114)
#define FLASH_HAEDVERSION_START (FLASH_SN_START + 16)               // 0x76000 + (292 - hex:0x124)
#define FLASH_IOS_VERSION_START (FLASH_HAEDVERSION_START + 16)      // 0x76000 + (308 - hex:0x134)
#define FLASH_SPORTSTATUS_START (FLASH_IOS_VERSION_START + 4)       // 0x76000 + (312 - hex:0x138)
#define FLASH_DATA_VERSION_START (FLASH_SPORTSTATUS_START + 4)      // 0x76000 + (316 - hex:0x13C)
#define FLASH_ANTI_LOST_START (FLASH_DATA_VERSION_START + 4)        // 0x76000 + (320 - hex:0x140)
#define FLASH_SYS_CONFIG_START (FLASH_ANTI_LOST_START + 4)

#endif
